
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class 哈希算法 {
    public static void main(String[] args) {
        test1();
        test2();
    }
    //验证哈希算法
    static void test1(){
        try{
            MessageDigest md=MessageDigest.getInstance("MD5");
            md.update("Hello".getBytes("UTF-8"));
            md.update("World".getBytes("UTF-8"));
            byte[] result=md.digest();
            System.out.println(new BigInteger(1,result).toString(16));
        }
        catch(NoSuchAlgorithmException e){
            System.out.println("抱歉，MD5不是有效的哈希算法");
        }
        catch(UnsupportedEncodingException e){
            System.out.println("不支持UTF-8编码方式");
        } 
    }
    //验证SHA-1算法
    static void test2(){
        try{
            MessageDigest md=MessageDigest.getInstance("SHA-1");
            md.update("hello".getBytes("UTF-8"));
            md.update("world".getBytes("UTF-8"));
            byte[] result=md.digest();
            System.out.println(new BigInteger(1,result).toString(16));
        }
        catch(NoSuchAlgorithmException e){
            System.out.println("抱歉，SHA-1不是有效的哈希算法");
        }
        catch(UnsupportedEncodingException e){
            System.out.println("不支持UTF-8编码方式");
        }
    }
}
